package main

import (
	"fmt"
	"reflect"
)

type Monster struct {
	Name string `tag:"name"`
	Age  int    `tag:"age"`
}

func (m Monster) Show() {
	fmt.Printf("%s is %d years old\n", m.Name, m.Age)
}

func main() {
	var m = Monster{
		Name: "牛魔王",
		Age:  150,
	}
	StructTest(&m)
}

func StructTest(s interface{}) {
	var rType = reflect.TypeOf(s)
	var rValue = reflect.ValueOf(s)
	var rKind = rValue.Kind()
	//fmt.Println(rKind)
	//fmt.Println(rValue.Elem().Kind())
	if rKind != reflect.Ptr || rValue.Elem().Kind() != reflect.Struct {
		fmt.Println("expect struct")
		return
	}
	for i := 0; i < rType.Elem().NumField(); i++ {
		tag := rType.Elem().Field(i).Tag.Get("tag")
		if tag == "" {
			continue
		}
		fmt.Println(tag)
		val := rValue.Elem().Field(i)
		switch val.Kind() {
		case reflect.Int:
			val.SetInt(15000)
		case reflect.String:
			val.SetString("红孩儿")

		}
	}
	rValue.Elem().Method(0).Call(nil)
}
